﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace STHPMS1.Web.W_Model
{
    /// <summary>
    /// 路段划分Model
    /// </summary>
    public class RoadDividedModel
    {
        public RoadDividedModel Prev { get; set; }
        public RoadDividedModel Next { get; set; }

        public string D_Rdwayid { get; set; }
        public DateTime D_Createdate { get; set; }
        public double D_Beginmp { get; set; }
        public double D_Endmp { get; set; }
        public int D_Type { get; set; }
    }


    public class RoadDividedModelLinked
    {
        private List<RoadDividedModel> _roads = new List<RoadDividedModel>();
        private List<double> _stake = new List<double>();

        public void Add(RoadDividedModel model)
        {
            _roads.Add(model);
            
            if(!_stake.Contains(model.D_Beginmp))
            {
                _stake.Add(model.D_Beginmp);
            }

            if (!_stake.Contains(model.D_Endmp))
            {
                _stake.Add(model.D_Endmp);
            }
        }

        public void AddStake(double value)
        {
            if (!_stake.Contains(value))
            {
                _stake.Add(value);
            }
        }

        public List<RoadDividedModel> BuildSection()
        {
            if (_stake == null || _stake.Count == 0) return null; ;

            List<RoadDividedModel> model = new List<RoadDividedModel>();
            _stake.Sort();

            double begin = _stake[0];
            string rdwayid=_roads[0].D_Rdwayid;

            for (int i = 1; i < _stake.Count; i++)
            {
                double end = _stake[i];
                var selRoads = _roads.Where(s => (s.D_Beginmp >= begin && s.D_Beginmp <= end) || (s.D_Endmp >= begin && s.D_Endmp <= end)).ToList();
                if (selRoads.Count == 0) continue;

                DateTime createDate = selRoads.Max(s=>s.D_Createdate);
                int type=selRoads[0].D_Type;
                foreach (var item in selRoads)
                {
                    type = type | item.D_Type;
                }

                RoadDividedModel r = new RoadDividedModel();
                r.D_Beginmp = begin;
                r.D_Endmp = end;
                r.D_Createdate = createDate;        
                r.D_Rdwayid = rdwayid;
                r.D_Type = type;

                model.Add(r);
                begin = end;
            }

            return model;
        }
    }
}